<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 0);

include_once("validate.php");

if(isset($_GET['id']) && $_GET['id'] == $properties['cron_id'])
{

	if(isset($_GET['profile']))
	{
		global $www_path, $lang;
	
		include_once('includes/email.php');
	
		if($_GET['profile'] == 'event_reminder')
		{
		
			// Create query
			$q = "SELECT e_id, e_title, t_fullTitle, t_location, t_logo, e_preevent, e_description, 
							 date_format(ed_date, '%M %e, %Y, %l:%i%p') as ed_dateF,
							 d_title, d_logo, c_logoURL, c_title, d.pf_drivername, u.user_email
				  FROM " . EVENTS_TABLE . ", " . EVENTDATES_TABLE . ", " . DIVISIONS_TABLE . ", " . CHAMPIONSHIPS_TABLE . ", " . 
					 	   TRACKS_TABLE . ", " . CHAMPSIGNUPS_TABLE . ", " . DRIVERNAME_TABLE . " d, " .  USERSGROUPS_TABLE . " g, " .
						   USERS_TABLE . " u 
				  WHERE e_id = ed_event AND ed_division = d_id AND d_champid = c_id AND e_track = t_id AND cs_div = d_id AND 
				  	 	u.user_id = cs_driver AND (pf_champ_notif IS NULL OR pf_champ_notif = 1) AND u.user_id = g.user_id AND 
						g.user_id = d.user_id AND g.group_id = 3 AND DATEDIFF(ed_date, NOW()) = 2 AND e_openSignUps = 1 AND 
						c_enabled = 1 AND cs_driver = cs_entryOwner AND cs_driver NOT IN (
						  SELECT es_driver
						  FROM lc_eventsignups
						  WHERE es_event = e_id)";

			// Run query
			$res = $db->sql_query($q);
			if ( $row = $db->sql_fetchrow($res) )
			{
				$email = new email('event_reminder', $row);
				
				$email->addReplacement('{ROUNDNAME}' , 'e_title', 1);
				$email->addReplacement('{TRACKNAME}' , 't_fullTitle', 1);
				$email->addReplacement('{LOCATION}' , 't_location', 1);
				$email->addReplacement('{EMAILADRESS}' , 'alex_sawczuk@hotmail.com');//'user_email', 1);
				$email->addReplacement('{DATE}' , 'ed_dateF', 1);
				$email->addReplacement('{DRIVERNAME}' , 'pf_drivername', 1);
				$email->addReplacement('{EVENTSIGNUPLINK}' , '<a href="' . $www_path . 'events.php?mode=entrants&id=' . $row['e_id'] . '">' . 
				 											 $www_path . 'events.php?mode=entrants&id=' . $row['e_id'] . '</a>' );
				if($row['c_title'] == $row['d_title'])
				{
					$email->addReplacement('{CHAMPIONSHIP}' , 'c_title', 1);
					$email->addReplacement('{CHAMPIONSHIP2}' , 'c_title', 1);
				}
				else
				{
					$email->addReplacement('{CHAMPIONSHIP}' , $row['c_title'] . '<br>' . $row['d_title']);
					$email->addReplacement('{CHAMPIONSHIP2}' , $row['c_title'] . ' ' . $row['d_title']);
				}
				
				if($row['d_logo'] != '')
				{
					$email->addReplacement('{CHAMPIONSHIPLOGO}' , '<img src="' . $www_path . $row['d_logo'] . '"/>' );
				}
				else if($row['c_logoURL'] != '')
				{
					$email->addReplacement('{CHAMPIONSHIPLOGO}' , '<img src="' . $www_path . $row['c_logoURL'] . '"/>' );
				}
				else
				{
					$email->addReplacement('{CHAMPIONSHIPLOGO}' , '' );
				}
				
				if($row['t_logo'] != '')
				{
					$email->addReplacement('{TRACKLOGO}' , '<img src="' . $www_path . $row['t_logo'] . '"/>' );
				}
				else
				{
					$email->addReplacement('{TRACKLOGO}' , '' );
				}
				
				
				
				
				$email->sendEmail();
			}
		}
		
		else if($_GET['profile'] == 'signup_reminder')
		{
		
			// Create query
			$q = "SELECT e_id, e_title, t_fullTitle, t_location, t_logo, e_preevent, e_description, 
							 date_format(ed_date, '%M %e, %Y, %l:%i%p') as ed_dateF,
							 d_title, d_logo, c_logoURL, c_title, d.pf_drivername, u.user_email
				  FROM " . EVENTS_TABLE . ", " . EVENTDATES_TABLE . ", " . DIVISIONS_TABLE . ", " . CHAMPIONSHIPS_TABLE . ", " . 
					 	   TRACKS_TABLE . ", " . EVENTSIGNUPS_TABLE . ", " . DRIVERNAME_TABLE . " d, " .  USERSGROUPS_TABLE . " g, " .
						   USERS_TABLE . " u 
				  WHERE e_id = ed_event AND ed_division = d_id AND d_champid = c_id AND e_track = t_id AND es_division = d_id AND 
				  	 	u.user_id = es_driver AND (pf_event_notif IS NULL OR pf_event_notif = 1) AND u.user_id = g.user_id AND 
						g.user_id = d.user_id AND g.group_id = 3 AND DATEDIFF(ed_date, NOW()) = 2 AND e_openSignUps = 1 AND 
						c_enabled = 1 AND  es_event = e_id";
						
			//echo $q;

			// Run query
			$res = $db->sql_query($q);
			if ( $row = $db->sql_fetchrow($res) )
			{
				$email = new email('signedup_reminder', $row);
				
				$email->addReplacement('{ROUNDNAME}' , 'e_title', 1);
				$email->addReplacement('{TRACKNAME}' , 't_fullTitle', 1);
				$email->addReplacement('{LOCATION}' , 't_location', 1);
				$email->addReplacement('{EMAILADRESS}' , 'alex_sawczuk@hotmail.com');//'user_email', 1);
				$email->addReplacement('{DATE}' , 'ed_dateF', 1);
				$email->addReplacement('{DRIVERNAME}' , 'pf_drivername', 1);
				$email->addReplacement('{MYEVENTSLINK}' , '<a href="' . $www_path . 'usercp.php?action=mycurrentevents">' . 
				 											 $www_path . 'usercp.php?action=mycurrentevents</a>' );
				if($row['c_title'] == $row['d_title'])
				{
					$email->addReplacement('{CHAMPIONSHIP}' , 'c_title', 1);
					$email->addReplacement('{CHAMPIONSHIP2}' , 'c_title', 1);
				}
				else
				{
					$email->addReplacement('{CHAMPIONSHIP}' , $row['c_title'] . '<br>' . $row['d_title']);
					$email->addReplacement('{CHAMPIONSHIP2}' , $row['c_title'] . ' ' . $row['d_title']);
				}
				
				if($row['d_logo'] != '')
				{
					$email->addReplacement('{CHAMPIONSHIPLOGO}' , '<img src="' . $www_path . $row['d_logo'] . '"/>' );
				}
				else if($row['c_logoURL'] != '')
				{
					$email->addReplacement('{CHAMPIONSHIPLOGO}' , '<img src="' . $www_path . $row['c_logoURL'] . '"/>' );
				}
				else
				{
					$email->addReplacement('{CHAMPIONSHIPLOGO}' , '' );
				}
				
				if($row['t_logo'] != '')
				{
					$email->addReplacement('{TRACKLOGO}' , '<img src="' . $www_path . $row['t_logo'] . '"/>' );
				}
				else
				{
					$email->addReplacement('{TRACKLOGO}' , '' );
				}
				
				
				
				
				$email->sendEmail();
			}
		}
	}

}

?>